package com.example.sale.Controller;

import com.example.sale.utils.UploadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//调用上传图片的接口
@CrossOrigin
@RestController
@RequestMapping("File")
public class UploadFileController {
    @PostMapping("/upImg")
    public String upImg(MultipartFile file) throws IOException {
        System.out.println("1111");
        System.out.println(file);
        return UploadUtil.uploadImage(file);
    }
    @PostMapping("/uploadImg")
    @ResponseBody
    public Map<String, Object> uploadImg(@RequestParam(value="myFileName") MultipartFile file, HttpServletRequest request) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Map<String, String> data = new HashMap<>();
            String filename = file.getOriginalFilename();//获取图片名
            String module = filename;
            String upload = UploadUtil.uploadImage(file);
            data.put("url",upload);//这里应该是项目路径，返回前台url
            data.put("alt",null);
            data.put("href",null);
            map.put("errno",0);
            map.put("data",data);
            System.out.println(map);
            System.out.println(upload);
            return map;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return  null;
        }
    }
}
